package org.tgcloud.group.service.manager;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.tgcloud.group.core.APPResultMessage;
import org.tgcloud.group.core.BeanUtil;
import org.tgcloud.group.core.ResultMessage;
import org.tgcloud.group.core.StringUtil;
import org.tgcloud.group.entity.MjNormalUser;
import org.tgcloud.group.entity.MjOtherProject;
import org.tgcloud.group.repository.MjNormalUserMapper;
import org.tgcloud.group.repository.MjOtherProjectMapper;
import org.tgcloud.group.repository.SysLabelValueMapper;
import org.tgcloud.group.repository.SysManagerUserMapper;
import org.tgcloud.group.web.controller.dto.otherProject.OtherProjectChannelMsgDTO;
import org.tgcloud.group.web.controller.dto.otherProject.OtherProjectChannelUserMsgDTO;
import org.tgcloud.group.web.controller.dto.otherProject.OtherProjectDTO;
import org.tgcloud.group.web.controller.dto.otherProject.OtherProjectManageMsgDTO;
import org.tgcloud.group.web.controller.req.otherProject.OtherProjectReq;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OtherProjectService {
    @Autowired
    private MjOtherProjectMapper otherProjectMapper;

    @Autowired
    private MjNormalUserMapper normalUserMapper;

    @Autowired
    private SysManagerUserMapper managerUserMapper;

    @Autowired
    private SysLabelValueMapper sysLabelValueMapper;

    public APPResultMessage<Object, OtherProjectDTO> getOtherProjectList(Integer pageNum, Integer pageSize, String keyword) {
        if (pageNum != null && pageSize != null){
            PageHelper.startPage(pageNum,pageSize);
            List<OtherProjectDTO> projects = otherProjectMapper.getList(keyword);
            PageInfo<OtherProjectDTO> tables = new PageInfo<>(projects);
            ResultMessage result = ResultMessage.builder().setList(tables.getList()).setAllPageNumber(tables.getPages()).setCount(tables.getTotal());
            APPResultMessage<Object, OtherProjectDTO> message = APPResultMessage.getSuccessMessage();
            message.setResult(result);
            return message;
        }else {
            List<OtherProjectDTO> users = otherProjectMapper.getList(keyword);
            ResultMessage resultMessage = ResultMessage.builder().setList(users);
            APPResultMessage<Object, OtherProjectDTO> successMessage = APPResultMessage.getSuccessMessage();
            successMessage.setResult(resultMessage);
            return successMessage;
        }
    }

    public APPResultMessage<Object, List> addOtherProject(OtherProjectReq req, int userId) {
        String addUserId = String.valueOf(req.getChoosePushUserId());
        MjOtherProject otherProject = new MjOtherProject();
        BeanUtils.copyProperties(req, otherProject);
        otherProject.setPushUserIds(addUserId);
        otherProject.setCreateTime(new Date());
        otherProject.setStatus((short) 1);
        otherProject.setCreateUserId(userId);
        otherProject.setCreateUserType(2);
        JSONObject userStatusMap = new JSONObject();
        userStatusMap.put("user_" + addUserId.toString(), 85);
        otherProject.setUserStatusMapJson(JSONObject.toJSONString(userStatusMap));
        otherProjectMapper.insertSelective(otherProject);
        return APPResultMessage.getSuccessMessage();
    }

    public APPResultMessage<Object, List> delOtherProject(Integer otherProjectId) {
        otherProjectMapper.updateStatusById(otherProjectId,(short) 10);
        return APPResultMessage.getSuccessMessage();
    }

    public APPResultMessage<OtherProjectManageMsgDTO, List> viewOtherProjectMsg(Integer id) {
        OtherProjectManageMsgDTO dto = new OtherProjectManageMsgDTO();
        MjOtherProject otherProject = otherProjectMapper.selectByPrimaryKey(id);
        BeanUtils.copyProperties(otherProject, dto);
        if (dto.getCreateUserType() == 1){
            dto.setCreateUsername(normalUserMapper.selectByPrimaryKey(otherProject.getCreateUserId()).getNickName());
        }else {
            dto.setCreateUsername(managerUserMapper.selectByPrimaryKey(otherProject.getCreateUserId()).getUsername());
        }
        if (StringUtil.isNotBlank(otherProject.getPushUserIds())){
            String[] split = otherProject.getPushUserIds().split(",");
            dto.setPushUsername(normalUserMapper.selectByPrimaryKey(Integer.valueOf(split[0])).getNickName());
        }
        List<OtherProjectChannelMsgDTO> channelFirstList = new ArrayList<>();
        List<OtherProjectChannelMsgDTO> channelSecondList = new ArrayList<>();
        if (StringUtil.isNotBlank(dto.getChannelPriceMapJson())){
            JSONObject channelPriceMapJsonObject = JSONObject.parseObject(otherProject.getChannelPriceMapJson());
            for (String key : channelPriceMapJsonObject.keySet()) {
                OtherProjectChannelUserMsgDTO channelUserMsgDTO = channelPriceMapJsonObject.getObject(key, OtherProjectChannelUserMsgDTO.class);
                OtherProjectChannelMsgDTO channelMsgDTO = new OtherProjectChannelMsgDTO();
                BeanUtils.copyProperties(channelUserMsgDTO, channelMsgDTO);
                channelMsgDTO.setChannelName(normalUserMapper.selectByPrimaryKey(channelUserMsgDTO.getUserId()).getNickName());
                channelMsgDTO.setPioneerName(normalUserMapper.selectByPrimaryKey(channelUserMsgDTO.getPushUserId()).getNickName());
                channelMsgDTO.setStatusName(sysLabelValueMapper.selectByPrimaryKey(channelMsgDTO.getStatus()).getSysValueName());
                if (channelUserMsgDTO.getPrice() != null && channelUserMsgDTO.getPrice().compareTo(BigDecimal.ZERO) > 0){
                    channelFirstList.add(channelMsgDTO);
                }
                if (channelUserMsgDTO.getSecondPrice() != null && channelUserMsgDTO.getSecondPrice().compareTo(BigDecimal.ZERO) > 0){
                    channelSecondList.add(channelMsgDTO);
                }
            }
        }
        if (!BeanUtil.isNullList(channelFirstList)){
            // 根据排名进行排序
            channelFirstList.sort((o1, o2) -> {
                if (o1.getRankNum() == null || o2.getRankNum() == null){
                    return 999;
                }else {
                    return o1.getRankNum().compareTo(o2.getRankNum());
                }
            });
            dto.setChannelFirstList(channelFirstList);
        }
        if (!BeanUtil.isNullList(channelSecondList)){
            channelSecondList.sort((o1, o2) -> {
                if (o1.getSecondRankNum() == null || o2.getSecondRankNum() == null){
                    return 999;
                }else {
                    return o1.getSecondRankNum().compareTo(o2.getSecondRankNum());
                }
            });
            dto.setChannelSecondList(channelSecondList);
        }
        APPResultMessage successMessage = APPResultMessage.getSuccessMessage();
        ResultMessage resultMessage = ResultMessage.builder().setData(dto);
        successMessage.setResult(resultMessage);
        return successMessage;
    }
}
